<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Kegiatan extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->_data["menuHL"] = "Kegiatan";
        $this->addBreadCrumb("Kegiatan", site_url("kegiatan"));
    }

    protected function _renderPage($page, $useLayout = true, $authorizedArr = Array()) {
        if ($useLayout) {
            $this->_data["page"] = $page;

            // kegByDates
            $this->load->model("Kegiatan_model");
            $allKegs = $this->Kegiatan_model->findAll(Array("is_approved" => true), true, 1);
            $kegByDates = Array();
            $kegByMonths = Array();
            $kegArr = Array();
            $hPlus = 0;
            foreach ($allKegs as $kegiatan) {
                $kegArr["" . $kegiatan->id] = Array(
                    "title" => $kegiatan->content_id->title,
                    "href" => site_url("kegiatan/detail/" . $kegiatan->id)
                );
                $hPlus = 0;
                $startDate = date($kegiatan->start_date);
                $endDate = date($kegiatan->end_date);
                $period = new DatePeriod(
                        new DateTime($startDate), new DateInterval('P1D'), new DateTime($endDate)
                );
                foreach ($period as $date) {
                    $dateString = $date->format('Y-m-d');
                    if (!array_key_exists($dateString, $kegByDates)) {
                        $kegByDates[$dateString] = Array();
                    }
                    array_push($kegByDates[$dateString], "H+" . $hPlus . " " . $kegiatan->content_id->title);
                    $hPlus++;
                    $monthString = $date->format('Y-m');
                    if (!array_key_exists($monthString, $kegByMonths)) {
                        $kegByMonths[$monthString] = Array();
                    }
                    if (!in_array($kegiatan->id, $kegByMonths[$monthString])) {
                        array_push($kegByMonths[$monthString], $kegiatan->id);
                    }
                }
                if ($startDate == $endDate) {
                    if (!array_key_exists($startDate, $kegByDates)) {
                        $kegByDates[$startDate] = Array();
                    }
                    array_push($kegByDates[$startDate], "H+" . $hPlus . " " . $kegiatan->content_id->title);
                    $monthString = substr($startDate, 0, 7);
                    if (!array_key_exists($monthString, $kegByMonths)) {
                        $kegByMonths[$monthString] = Array();
                    }
                    if (!in_array($kegiatan->id, $kegByMonths[$monthString])) {
                        array_push($kegByMonths[$monthString], $kegiatan->id);
                    }
                } else {
                    if (!array_key_exists($endDate, $kegByDates)) {
                        $kegByDates[$endDate] = Array();
                    }
                    array_push($kegByDates[$endDate], "H+" . $hPlus . " " . $kegiatan->content_id->title);
                    $monthString = substr($endDate, 0, 7);
                    if (!array_key_exists($monthString, $kegByMonths)) {
                        $kegByMonths[$monthString] = Array();
                    }
                    if (!in_array($kegiatan->id, $kegByMonths[$monthString])) {
                        array_push($kegByMonths[$monthString], $kegiatan->id);
                    }
                }
            }
            $this->_data["kegByDates"] = $kegByDates;
            $this->_data["kegByMonths"] = $kegByMonths;
            $this->_data["kegArr"] = $kegArr;

            parent::_renderPage("kegiatan/base", $authorizedArr);
        } else {
            parent::_renderPage($page, $authorizedArr);
        }
    }

    public function index() {
        redirect("kegiatan/berita");
    }

    public function berita() {
        $this->_data["useSlideShow"] = true;
        $this->addBreadCrumb("Berita Kegiatan", site_url("kegiatan/berita"));
        $this->load->model("Kegiatan_model");
        $this->load->model("Beritakegiatan_model");

        // hlKegiatans
        $hlKegiatans = $this->Kegiatan_model->fetchHL();
        $this->_data["hlKegiatans"] = $hlKegiatans;

        // pics
        $picList = $this->Beritakegiatan_model->fetchRecentPics();
        $this->_data["picList"] = $picList;

        $this->_renderPage("berita");
    }

    public function video() {
        $this->addBreadCrumb("Live Report Kegiatan", site_url("kegiatan/video"));
        $this->_data["titleFB"] = "Live Streaming Channel Perdesaan Sehat";
        $this->_data["imageFB"] = res_url("img/livestream.jpg");
        $this->_renderPage("video");
    }

    public function rekap() {
        $this->addBreadCrumb("Rekapitulasi Kegiatan", site_url("kegiatan/rekap"));
        $this->_data["useMap"] = true;

        $this->_renderPage("rekap");
    }

    // halaman view detail kegiatan
    public function detail($kegId, $newsId = -1) {
        $this->load->model("Kegiatan_model");
        $kegiatan = $this->Kegiatan_model->findById($kegId, true, 2);
        if ($kegiatan != NULL && ($kegiatan->is_approved || ($kegiatan->content_id->poster_id->id == $this->_loggedUser->id))) {
            $this->_data["kegiatan"] = $kegiatan;
            $this->load->model("Beritakegiatan_model");
            $this->_data["beritaList"] = $this->Beritakegiatan_model->findAll(Array("activity_id" => $kegiatan->id, "is_approved" => true), true, 1);
            if ($newsId < 0) { // detail kegiatan
                $this->addBreadCrumb($kegiatan->content_id->title, site_url("kegiatan/detail/" . $kegId));
                $this->_data["pageTitle"] = $kegiatan->content_id->title;
                $this->_renderPage("detailkegiatan");
            } else { // detail berita
                $this->addBreadCrumb($kegiatan->content_id->title, site_url("kegiatan/detail/" . $kegId));
                $this->load->model("Beritakegiatan_model");
                $berita = $this->Beritakegiatan_model->findById($newsId, true, 2);
                if ($berita != NULL && ($berita->is_approved || ($berita->content_id->poster_id->id == $this->_loggedUser->id))) {
                    $this->addBreadCrumb("Berita Kegiatan", site_url("kegiatan/detail/" . $kegId));
                    $this->_data["berita"] = $berita;
                    $this->_renderPage("detailberita");
                } else {
                    redirect("home/error");
                }
            }
        } else {
            redirect("home/error");
        }
    }

    /// ==== NEED AUTHORIZATION PAGES ====
    // save handling untuk create kegiatan
    public function saveKegiatan() {
        $authorized = Array("kegiatan_contributor");
        if ($this->isAuth($authorized)) {
            $this->load->model("Image_model");
            $this->load->model("Kegiatan_model");

            $thumbId = $this->Image_model->create($this->input->post("thumb"), "", Array());
            $bannerId = $this->Image_model->create($this->input->post("banner"), "", Array());
            $kegId = $this->Kegiatan_model->create(
                    $this->input->post("judul"), $this->input->post("konten"), $thumbId, $bannerId, $this->input->post("lokasi"), $this->input->post("tglmulai"), $this->input->post("tglselesai"), Array()
            );

            echo $kegId;
        }
        exit;
    }

    // save handling untuk create berita kegiatan
    public function saveBeritaKegiatan() {
        $authorized = Array("kegiatan_contributor");
        if ($this->isAuth($authorized)) {
            $pics = Array();
            $this->load->model("Image_model");
            for ($i = 0; $i < 5; $i++) {
                $tmp = $this->input->post("pic" . $i);
                if ($tmp != NULL) {
                    array_push($pics, $this->Image_model->create($tmp, "", Array()));
                }
            }

            $this->load->model("Beritakegiatan_model");
            $kegId = $this->input->post("kegiatan_id");
            $beritaId = $this->Beritakegiatan_model->create(
                    $kegId, $this->input->post("judul"), $this->input->post("konten"), Array(), $pics
            );

            echo $beritaId;

            exit;
        }
    }

    public function saveHLKegiatan() {
        $authorized = Array("kegiatan_auditor");
        if ($this->isAuth($authorized)) {
            $this->load->model("Kegiatan_model");
            $kegId = $this->input->post("kegiatan_id");
            return $this->Kegiatan_model->makeHL($kegId);
        }
    }

    // approve dan update kegiatan
    public function updateApproveKegiatan() {
        $authorized = Array("kegiatan_auditor", "master_admin");
        if ($this->isAuth($authorized)) {
            $this->load->model("Kegiatan_model");
            $this->load->model("Content_model");

            $kegObj = $this->Kegiatan_model->findById($this->input->post("kegiatan_id"));
            if ($kegObj != NULL) {
                if ($this->Content_model->updateById($kegObj->content_id, Array(
                            "title" => $this->input->post("title"),
                            "text" => $this->input->post("text")
                        ))) {
                    if ($this->Kegiatan_model->updateById($kegObj->id, Array("is_approved" => true))) {
                        echo 1;
                        exit;
                    }
                }
            }
        }
        echo 0;
    }

    public function deleteKegiatan() {
        $authorized = Array("kegiatan_auditor", "master_admin");
        if ($this->isAuth($authorized)) {
            $this->load->model("Kegiatan_model");
            if ($this->Kegiatan_model->deleteById($this->input->post("kegiatan_id"))) {
                echo 1;
                exit;
            }
        }
        echo 0;
    }

    public function updateApproveBeritaKegiatan() {
        $authorized = Array("kegiatan_auditor", "master_admin");
        if ($this->isAuth($authorized)) {
            $this->load->model("Beritakegiatan_model");
            $this->load->model("Content_model");

            $brtObj = $this->Beritakegiatan_model->findById($this->input->post("berita_id"));
            if ($brtObj != NULL) {
                if ($this->Content_model->updateById($brtObj->content_id, Array(
                            "title" => $this->input->post("title"),
                            "text" => $this->input->post("text")
                        ))) {
                    if ($this->Beritakegiatan_model->updateById($brtObj->id, Array("is_approved" => true))) {
                        echo 1;
                        exit;
                    }
                }
            }
        }
        echo 0;
    }

    public function deleteBeritaKegiatan() {
        $authorized = Array("kegiatan_auditor", "master_admin");
        if ($this->isAuth($authorized)) {
            $this->load->model("Beritakegiatan_model");
            if ($this->Beritakegiatan_model->deleteById($this->input->post("berita_id"))) {
                echo 1;
                exit;
            }
        }
        echo 0;
    }

}
